home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Java / Java_File_Save.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  5.4 KB  |  150 lines

  1. /*
  2.  * Java_File_Save.bsh - a BeanShell macro for saving new java files.
  3.  *
  4.  * Copyright (C) 2004 Nicholas O'Leary nol@deferential.net
  5.  * 
  6.  * :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=true:
  7.  * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  8.  *
  9.  * {{{ License
  10.  * This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License
  12.  * as published by the Free Software Foundation; either version 2
  13.  * of the License, or any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with the jEdit program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  23.  * }}}
  24.  *
  25.  * Notes:
  26.  *  Only the first 250 lines of the buffer are scanned for a suitable
  27.  *  class or interface declaration.
  28.  *  
  29.  * Changes:
  30.  *  17-May-04: Only scans if the edit mode is either 'java' or the default mode
  31.  *           : Ignores declarations that are in multiline comments
  32.  *  08-Jun-04: If an infinite loop is hit (1000 iterations) in the comment
  33.  *           : parsing, it now opens the default save dialog, rather than
  34.  *           : just returning.
  35.  * $Id: Java_File_Save.bsh,v 1.1 2004/06/26 19:10:58 spestov Exp $
  36.  */
  37.  
  38.  
  39. // Check this is a new file
  40. if (buffer.isNewFile() && buffer.getPath() != null)
  41. {
  42.    // Only look further if the mode is 'java', or still the default
  43.    String buffer_mode = buffer.getMode().toString();
  44.    if (buffer_mode.equals("java") || buffer_mode.equals(jEdit.getProperty("buffer.defaultMode","")))
  45.    {
  46.       String fullpath = buffer.getPath();
  47.       VFS vfs = VFSManager.getVFSForPath(fullpath);
  48.       // Split into constituent parts
  49.       String path = vfs.getParentOfPath(fullpath);
  50.       String name = vfs.getFileName(fullpath);
  51.       
  52.       // At most, check the first 250 lines - this sounds reasonable to me
  53.       int maxLine = Math.min(buffer.getLineCount(),250);
  54.       import gnu.regexp.RE;
  55.       import gnu.regexp.REMatch;
  56.       // Build the regex - based on the offical java language spec.
  57.       RE regex = new RE("^\\s*(public|protected|private|static|abstract|final|native|synchronized|transient|volatile|strictfp)?\\s*(class|interface)\\s*([^ {/]*)");
  58.       int regexMinimum = regex.getMinimumLength();
  59.       boolean inComment = false;
  60.       for(int i=0;i<maxLine;i++)
  61.       {
  62.          String txt = buffer.getLineText(i);
  63.          int count = 0;
  64.             // See if this line has a the start or finish of a multiline comment
  65.          while (txt.indexOf("/*")!=-1 || txt.indexOf("*/")!=-1)
  66.             {
  67.                 // A little paranoia on my part 
  68.             count++;
  69.             if (count==1000)
  70.             {
  71.                Log.log(Log.ERROR,BeanShell.class,"Infinite loop:["+txt+"]");
  72.                buffer.save(view,null,true);
  73.                return;
  74.             }
  75.                 // Look for the next starting comment if we're not in a comment
  76.             if (!inComment)
  77.             {
  78.                int commentStartIndex = txt.indexOf("/*");
  79.                if (commentStartIndex != -1)
  80.                {
  81.                   inComment = true;
  82.                   if (commentStartIndex+2 == txt.length())
  83.                      txt = "";
  84.                   else
  85.                      txt = txt.substring(commentStartIndex+2);
  86.                }
  87.             }
  88.                 // Look for the next ending comment if we are in a comment
  89.             if (inComment)
  90.             {
  91.                int commentEndIndex = txt.indexOf("*/");
  92.                if (commentEndIndex != -1)
  93.                {
  94.                   inComment = false;
  95.                   if (commentEndIndex+2 == txt.length())
  96.                      txt = "";
  97.                   else
  98.                      txt = txt.substring(commentEndIndex+2);
  99.                } else {
  100.                   continue;
  101.                }
  102.             }
  103.          }
  104.             
  105.             // We now know if the remainder of the line is in a comment or not
  106.          if (!inComment)
  107.          {
  108.             // Ignore lines that are too short for the regex
  109.             if (txt.length() < regexMinimum)
  110.                continue;
  111.             REMatch match = regex.getMatch(txt);
  112.             // See if it matches
  113.             if (match!=null)
  114.             {
  115.                int startIndex = match.getStartIndex(3);
  116.                int endIndex = match.getEndIndex(3);
  117.                // Extract the class/interface name
  118.                name = txt.substring(startIndex,endIndex)+".java";
  119.                break;
  120.             }
  121.          }
  122.       }
  123.       
  124.       // Open the VFSBrowser
  125.       String[] files = GUIUtilities.showVFSFileDialog(view,path+name,
  126.                                                 VFSBrowser.SAVE_DIALOG,false);
  127.       if(files == null)
  128.          return false;
  129.       buffer.save(view,files[0],true);
  130.       return;
  131.    }
  132. }
  133.  
  134. // This isn't a file that has been scanned, so just do a normal save
  135. buffer.save(view,null,true);
  136.  
  137. /*
  138.  
  139. Macro index data (in DocBook format)
  140.  
  141.    <listitem>
  142.       <para><filename>Java_File_Save.bsh</filename></para>
  143.       <abstract><para>Acts as a wrapper script to the Save As action. If the buffer
  144.       is a new file, it scans the first 250 lines for a Java class or interface
  145.       declaration. On finding one, it extracts the appropriate filename to be
  146.       used in the Save As dialog.</para></abstract>   
  147.       </listitem>
  148.  
  149. */
  150.